package com.tocean.demo.service;

import com.tocean.demo.entity.questionbank.TestRelation;
import com.tocean.demo.repository.TestRlationRepository;
import com.tocean.demo.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TestRelationService {

    @Autowired
    private TestRlationRepository testRelationRepository;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    public void addRelation(String tid,String qid){
        TestRelation testRelation = new TestRelation();
        int size = testRelationRepository.findAll().size();
        if(size == 0){
            testRelation.setId(snowflakeIdWorker.nextId());
            testRelation.setTestid(Long.valueOf(tid));
            testRelation.setQuestionid(Long.valueOf(qid));
            testRelationRepository.save(testRelation);
        }
        else if(size!=0){
            //List<TestRelation> test = testRelationRepository.findAllByTestid(Long.valueOf(tid));
            TestRelation test = testRelationRepository.findByTestidAndQuestionid(Long.valueOf(tid),Long.valueOf(qid));
            if(test == null){
                testRelation.setId(snowflakeIdWorker.nextId());
                testRelation.setTestid(Long.valueOf(tid));
                testRelation.setQuestionid(Long.valueOf(qid));
                testRelationRepository.save(testRelation);
            }
        }
    }


}
